%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Full Name [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for the generic search module {@link search}.
%%% @see search
%%%
%%% @todo(jacoby) Add tests to cover cyclic graphs (not required
%%% for TXM)


-module(search_tests).

-include_lib("eunit/include/eunit.hrl").

-export([eval/2, goal_reached/2, successors/2]).

%%% ---------------------------------------------------------------------------
%%% Search tests with context
%%% ---------------------------------------------------------------------------

%% @doc Test case where initial state is goal.
base_search_test() ->
  Result = search:search(four, {?MODULE, some_context}),
  ?assertEqual([four], Result).

%% @doc Test case where steps are required and eval
%% indicates preferred route.
simple_search_test() ->
  Result = search:search(one, {?MODULE, some_context}),
  ?assertEqual([one, three, four], Result).

%% @doc Test case where no goal can be reached.
failed_search_test() ->
  Result = search:search(five, {?MODULE, some_context}),
  ?assertEqual([], Result).

%%% ---------------------------------------------------------------------------
%%% Search tests without context
%%% ---------------------------------------------------------------------------

%% @doc Test case where initial state is goal.
base_search_nc_test() ->
  Result = search:search(four, ?MODULE),
  ?assertEqual([four], Result).

%% @doc Test case where steps are required and eval
%% indicates preferred route.
simple_search_nc_test() ->
  Result = search:search(one, ?MODULE),
  ?assertEqual([one, three, four], Result).

%% @doc Test case where no goal can be reached.
failed_search_nc_test() ->
  Result = search:search(five, ?MODULE),
  ?assertEqual([], Result).

%% Define a little acyclic graph which looks like this (goals
%% are capitalized, evals are 1 except where noted):
%%
%% one +-- two(10) ---+
%%     |    |         |
%%     |    |         |
%%     +-- three(5)   |
%%          |         |
%%          +------ FOUR -- SIX
%%          |
%%          +-- five +-- seven
%%                   |
%%                   +-- eight
%%
goal_reached(four, _Context) -> true;
goal_reached(six, _Context) -> true;
goal_reached(_Other, _Context) -> false.

successors(one, _Context) -> [two, three];
successors(two, _Context) -> [three, four];
successors(three, _Context) -> [four, five];
successors(four, _Context) -> [six];
successors(five, _Context) -> [seven, eight];
successors(_Other, _Context) -> [].

eval(two, _Context) -> 10;
eval(three, _Context) -> 5;
eval(_Other, _Context) -> 1.
